﻿<Window x:Class="Brjnk.GpxExplorer.UI.SearchPlaceWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:Brjnk.GpxExplorer.Localization"
        xmlns:my="clr-namespace:Brjnk.GpxExplorer.UI"
        xmlns:my1="clr-namespace:Brjnk.GpxExplorer.UI.Controls"
        xmlns:searching="clr-namespace:Brjnk.GpxExplorer.Workspace.Searching"
        xmlns:ui="clr-namespace:Brjnk.GpxExplorer.UI"
        FocusManager.FocusedElement="{Binding ElementName=txbInput}"
        Title="{l:Loc titleSearchPlace}"
        Width="500"
        Height="400"
        MinWidth="300"
        MinHeight="200"
        DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <Window.Resources>
        <XmlDataProvider x:Key="xmlData" IsAsynchronous="True" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Image Grid.ColumnSpan="2"
               Cursor="Hand"
               MouseDown="Image_MouseDown"
               Source="/Resources/Icons/GeoNames.png"
               Stretch="None" />

        <Grid Grid.Row="1" Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBox Grid.Row="0"
                     Grid.ColumnSpan="4"
                     Name="txbInput"
                     Style="{StaticResource HeaderTextBoxStyle}"
                     Text="{Binding SearchedString}" />
            <Button Grid.Row="0"
                    Grid.RowSpan="2"
                    Grid.Column="4"
                    Click="Button_Click"
                    IsDefault="True"
                    Content="{l:Loc searchPlaceSearch}"
                    Name="searchButton" />

            <TextBlock Grid.Row="1" Text="{l:Loc searchPlaceMode}" />
            <my1:EnumPicker Grid.Row="1"
                            Grid.Column="1"
                            HorizontalAlignment="Left"
                            EnumType="{x:Type ui:SearchPlaceMode}"
                            Value="{Binding Mode}" />
            <CheckBox Name="chbxCountry"
                      Grid.Row="1"
                      Grid.Column="2"
                      Content="{l:Loc searchPlaceCountry}"
                      IsChecked="{Binding FilterByCountry}" />
            <ComboBox Grid.Row="1"
                      Grid.Column="3"
                      HorizontalAlignment="Left"
                      IsEnabled="{Binding ElementName=chbxCountry,
                                          Path=IsChecked}"
                      ItemsSource="{Binding Regions}"
                      SelectedItem="{Binding SelectedRegion}" />
        </Grid>

        <ListView Name="items"
                  Grid.Row="3"
                  Grid.ColumnSpan="2"
                  AlternationCount="2"
                  ItemsSource="{Binding Source={StaticResource xmlData},
                                        XPath=geonames/geoname}">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.Style>
                            <Style TargetType="Grid">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}, Path=(ItemsControl.AlternationIndex)}" Value="1">
                                        <Setter Property="Background" Value="{StaticResource AlternationBrush}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.ColumnSpan="3"
                                   FontWeight="Bold"
                                   Text="{Binding XPath=toponymName}" />

                        <my:GoToCoordinatesPanel
                            ButtonAddPushpin="False"
                            Grid.Row="0" Grid.ColumnSpan="3" HorizontalAlignment="Right"
                            GpsCoordinates="{Binding Converter={StaticResource GeoNamesXmlToCoordinatesConverter}}"
                            PushpinName="{Binding XPath=toponymName}" />

                        <TextBlock Grid.Row="1"
                                   Grid.Column="0"
                                   Text="{Binding XPath=countryName}" />
                        <TextBlock Grid.Row="1"
                                   Grid.Column="1"
                                   Text="{Binding XPath=lat}" />
                        <TextBlock Grid.Row="1"
                                   Grid.Column="2"
                                   Text="{Binding XPath=lng}" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <my1:ColorPicker Grid.Row="4"
                         Width="150"
                         Margin="3"
                         HorizontalAlignment="Left"
                         Color="{Binding SearchRequestColor}" />

        <my:SearchPositionArgsEditor x:Name="searchArgs"
                                     Grid.Row="5"
                                     Grid.ColumnSpan="2" />

        <CheckBox Grid.Row="6"
                  Content="{l:Loc searchPlaceCreateWaypoint}"
                  IsChecked="{Binding CreateWaypoint}" />

        <StackPanel Grid.Row="7"
                    Grid.ColumnSpan="2"
                    HorizontalAlignment="Right"
                    Orientation="Horizontal">
            <Button HorizontalAlignment="Right"
                    Click="Button_Click_1"
                    Content="{l:Loc cancel}" />
            <Button Click="Button_Click_2" Content="{l:Loc ok}">
                <Button.Style>
                    <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=searchArgs, Path=LimitValidationFailed}" Value="True">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=items, Path=SelectedItems.Count}" Value="0">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </StackPanel>
    </Grid>
</Window>